const baseUrl = 'https://ivu06wjqra.dy.takin.cc/ai'
const header = {
	Authorization: ""
}
let isModalShown = true
const request = (url = '', type = 'post', date = {}) => {
	const userinfo = uni.getStorageSync('digital_aI_key')
	header.Authorization = userinfo ? userinfo.token : ''
	return new Promise((resolve, reject) => {
		uni.request({
			method: type,
			url: baseUrl + url,
			data: date,
			header: header,
			dataType: 'json',
		}).then((response) => {

			if (response.data.code == 201) {
				uni.showModal({
					title: '登录提示',
					content: '您的登录已过期,请重新登录',
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.removeStorageSync('digital_aI_key');
							// uni.reLaunch({
							// 	url: '/subpackage/myhome/myhome'
							// });

						}
					}
				});
				reject('用户未登录');
				return;
			}
			resolve(response);

		}).catch(error => {
			reject(error)
		})
	});
}
const uploadimg = (filePath,url) => {
	const userinfo = uni.getStorageSync('digital_aI_key')
	header.Authorization = userinfo.token
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl + url,
			filePath: filePath,
			header: {
				Authorization: header.Authorization
			},
			name: 'file', // 后台接收文件的字段名
			success: (res) => {
				console.log(res);
				const url =JSON.parse(res.data).data;
				resolve(url); // 将上传文件的URL传递给Promise对象
			},
			fail: (err) => {
				console.log(err);
				reject(err);
			}
		});
	});
}


export default {
	request,
	baseUrl,
	uploadimg

};